import {
	service
} from '@/service/service.js'

const state = {
	// 登陆状态
	isLogin: false,
	// 用户信息
	userInfo: {
		id: 0,
		name: 'Guest',
	},
}

const getters = {
	isLogin(state) {
		return state.isLogin
	},
	userInfo(state) {
		return state.userInfo
	},
}

const actions = {
	/**
	 * @desc 登陆
	 * @param {*} param0 
	 * @param {*} payload 
	 */
	async login({
		commit
	}, payload) {
		const res = await service('auth.login', payload)
		if (res.data.code === 'success') {
			if (Object.keys(res.data.data).length) {
				commit('setUserInfo', res.data.data)
				commit('setIsLogin', true)
			}
		}
	},

	/**
	 * @desc 注册
	 * @param {*} param0 
	 * @param {*} payload 
	 */
	async register({
		commit
	}, payload) {

	}
}

const mutations = {
	/**
	 * @desc 存储用户信息
	 * @param {*} state 
	 * @param {*} payload 
	 */
	setUserInfo(state, payload) {
		state.userInfo = payload
	},

	/**
	 * @desc 变更登陆状态
	 * @param {*} state 
	 * @param {*} payload 
	 */
	setIsLogin(state, payload) {
		state.isLogin = payload
	}
}

export default {
	namespaced: true,
	state,
	getters,
	actions,
	mutations
}
